//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "DtwGame.h"


//------------------------------------------------------------------------------
//=== constructor ===//
DtwStateMenu::DtwStateMenu()
	: m_poGuiSkin(0)
	, m_poGameMgr(0)
	, m_eCurMenuState( eMenuStateInitMain )		// current state
	, m_eTargetMenuState( eMenuStateNone )	// next state if any
	, m_iScnSizeX(0)			// width of display
	, m_iScnSizeY(0)			// height of display
	, m_f32ScaleWidth(1.0f)				// scale of width of display
	, m_f32ScaleHeight(1.0f)				// scale of height of display
	, m_eDefaultFontSize( eGuiFontSmall )
{
	memset( m_apoGuiImages, 0, sizeof( m_apoGuiImages ));
	memset( m_apoMenuSounds, 0, sizeof( m_apoMenuSounds ));
	m_au8QwertyKeys[0] = 'Q';
	m_au8QwertyKeys[1] = 'W';
	m_au8QwertyKeys[2] = 'E';
	m_au8QwertyKeys[3] = 'R';
	m_au8QwertyKeys[4] = 'T';
	m_au8QwertyKeys[5] = 'Y';
	m_au8QwertyKeys[6] = 'U';
	m_au8QwertyKeys[7] = 'I';
	m_au8QwertyKeys[8] = 'O';
	m_au8QwertyKeys[9] = 'P';
	m_au8QwertyKeys[10] = 'A';
	m_au8QwertyKeys[11] = 'S';
	m_au8QwertyKeys[12] = 'D';
	m_au8QwertyKeys[13] = 'F';
	m_au8QwertyKeys[14] = 'G';
	m_au8QwertyKeys[15] = 'H';
	m_au8QwertyKeys[16] = 'J';
	m_au8QwertyKeys[17] = 'K';
	m_au8QwertyKeys[18] = 'L';
	m_au8QwertyKeys[19] = 'Z';
	m_au8QwertyKeys[20] = 'X';
	m_au8QwertyKeys[21] = 'C';
	m_au8QwertyKeys[22] = 'V';
	m_au8QwertyKeys[23] = 'B';
	m_au8QwertyKeys[24] = 'N';
	m_au8QwertyKeys[25] = 'M';
	m_au8QwertyKeys[26] = '<';
	m_au8QwertyKeys[27] = 0;

}
//------------------------------------------------------------------------------
//=== destructor ===//
DtwStateMenu::~DtwStateMenu()
{
}
//------------------------------------------------------------------------------
//=== state overrides ===//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! startup
RCODE DtwStateMenu::GameStateStartup( VxIrrBase * poIrrBase )
{

	GameStateBase::GameStateStartup( poIrrBase );
	//log_msg( 0, "1 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );

	m_poGameMgr = (DtwGameMgr *)poIrrBase;
	m_iScnSizeX = m_poIrrBase->m_iScnSizeX;			// width of display
	m_iScnSizeY = m_poIrrBase->m_iScnSizeY;			// height of display
	log_msg( 0, "DtwStateMenu::GameStateStartup: starting w %d h %d\n", m_iScnSizeX, m_iScnSizeY );
	// menu design was based on android HVGA ( 320x480 )
	// we must scale it to other dimensions
	// HVGA (320x480), 3.0"-3.5" diagonal
	// WVGA (480x800), 4.8"-5.5" diagonal
    // FWVGA (480x854), 5.0"-5.8" diagonal
	m_f32ScaleWidth = (float)m_iScnSizeX/320.0f;
	m_f32ScaleHeight = (float)m_iScnSizeY/480.0f;


	// choose font corresponding to scale
	m_eDefaultFontSize = eGuiFontSmall;
	if( m_iScnSizeX > 400 )
	{
		m_eDefaultFontSize = eGuiFontLarge;
	}
	if( m_iScnSizeX > 500 )
	{
		m_eDefaultFontSize = eGuiFontLarge;
	}

	m_poGuiSkin = m_poGui->getSkin();

	LoadMenuResources( false );


	// load sounds that may be used
	//log_msg( 1, "DtwStateMenu::GameStateStartup loading sound eMenuSoundClick button click\n" );

	//g_oSndMgr.LoadBuffer( "button25.ogg" );
	m_apoMenuSounds[ eMenuSoundClick ] = new AudioSource( g_oSndMgr.GetBuffer( "button24.ogg" ) );
	//log_msg( 1, "DtwStateMenu::GameStateStartup AudioSource has sound %s\n", m_apoMenuSounds[ eMenuSoundClick ]->m_strSndFile.c_str() );
	//log_msg( 1, "DtwStateMenu::GameStateStartup done loading sound eMenuSoundClick button click\n" );

	// default to menu skin
	ChangeSkin( eGuiSkinTypeMenu );
	m_oTutorialTextInfo.m_u32DeleteTime = 0;

	m_poGameMgr->SetMouseLocked(false);

	if( eMenuStateNone != m_eTargetMenuState ) 
	{
		// caller preset which state to go to
		m_eCurMenuState = m_eTargetMenuState;
		m_eTargetMenuState = eMenuStateNone;
	}

	log_msg( 0, "DtwStateMenu::GameStateStartup: success\n" );
	//log_msg( 0, "Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
	return 0;
}


//------------------------------------------------------------------------------
//! shutdown
void DtwStateMenu::GameStateShutdown( void )
{
	//log_msg( 0, "20 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
	// clear out any buttons etc from gui
	ClearPreviousMenu();
	if( m_apoMenuSounds[ eMenuSoundClick ] )
	{
		delete m_apoMenuSounds[ eMenuSoundClick ];
		m_apoMenuSounds[ eMenuSoundClick ] = NULL;
	}
	//log_msg( 0, "9 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
}
//------------------------------------------------------------------------------
// update timed elements
RCODE DtwStateMenu::GameStateUpdate( U32 u32FrameTimeMs ) 
{
	// check if time to delete popup text
	m_u32Timer += u32FrameTimeMs;
	if( m_oTutorialTextInfo.m_strText.size() > 0 ) 
	{
		if( m_u32Timer >= m_oTutorialTextInfo.m_u32DeleteTime ) 
		{
			m_oTutorialTextInfo.m_strText = "";
		}
	}
	switch( m_eCurMenuState )
	{
	case eMenuStateInitMain: // initialize main menu
		//log_msg( 0, "2 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
		return InitMenuMain();
	case eMenuStateInitChooseDifficulty: // show dlg to choose difficulty level game
		//log_msg( 0, "3 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
		return InitMenuChooseDifficulty();
	case eMenuStateInitHighScores:
		//log_msg( 0, "4 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
		return InitMenuHighScores();
	case eMenuStateInitOptions:
		//log_msg( 0, "5 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
		return InitMenuOptions();
	case eMenuStateInitVideo:
		//log_msg( 0, "6 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
		return InitMenuVideo();
	case eMenuStateInitAudio:
		return InitMenuAudio();
	case eMenuStateInitTutorial:
		return InitMenuTutorial();
	case eMenuStateInitGameOver:
		return InitGameOver();
	case eMenuStateInitGameOverHighScore:
		return InitGameOverHighScore();
	case eMenuStateInitAddHighScore:
		return InitAddHighScore();
	case eMenuStateInitHighScoreLevel:
		return InitHighScoreLevel();


	case eMenuStateInitGame:
		return 0;

	default:
		break;
	}

	RenderMenuFrame();

	return 0;
}
//------------------------------------------------------------------------------
//! called when application is resuming from sleep.. app must reload textures etc
RCODE DtwStateMenu::GameStateAppResume( U32 u32FrameTimeMs )
{
	return LoadMenuResources( true );
}
//------------------------------------------------------------------------------
//! load textures for menu
//! bReloadFromResume = true if reloading after application resume
RCODE DtwStateMenu::LoadMenuResources( BOOL bReloadFromResume )
{
	//log_msg( 0, "7 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
	if( bReloadFromResume )
	{
		// drop fonts
		m_apoFonts[ eGuiFontSmall ]->drop();
		m_apoFonts[ eGuiFontMedium ]->drop();
		m_apoFonts[ eGuiFontLarge ]->drop();

		// drop old textures
		m_apoGuiImages[ eGuiImageMouseCursor ]->drop();
		m_apoGuiImages[ eGuiImageBlack ]->drop();
		m_apoGuiImages[ eGuiImageTextBoxCorner ]->drop();
		m_apoGuiImages[ eGuiImageTextBoxLeftEdge ]->drop();
		m_apoGuiImages[ eGuiImageTextBoxTopEdge ]->drop();
		m_apoGuiImages[ eGuiImageButton80x32 ]->drop();
		m_apoGuiImages[ eGuiImageButton100x32 ]->drop();
		m_apoGuiImages[ eGuiImageButton128x32 ]->drop();
	}

	//=== load resources ===//
	// load fonts
	m_apoFonts[ eGuiFontSmall ] = m_poGui->getFont( GetAssetPathAndName( "font_small.xml", eGameAssetTypeGui ) );
	if( NULL == m_apoFonts[ eGuiFontSmall ] ) 
	{
		log_msg( 0, "DtwStateMenu::GameStateStartup: failed loading %s\n", GetAssetPathAndName( "font_small.xml", eGameAssetTypeGui ).c_str() );
		return -1;
	}
	// small font is default
	m_poGuiSkin->setFont( m_apoFonts[ eGuiFontSmall ] );

	m_apoFonts[ eGuiFontMedium ] = m_poGui->getFont( GetAssetPathAndName( "font_medium.xml", eGameAssetTypeGui ) );
	if( NULL == m_apoFonts[ eGuiFontMedium ] ) 
	{
		log_msg( 0, "DtwStateMenu::GameStateStartup: failed loading font_medium.xml\n" );
		return -1;
	}

	m_apoFonts[ eGuiFontLarge ] = m_poGui->getFont( GetAssetPathAndName( "font_large.xml", eGameAssetTypeGui ) );
	if( NULL == m_apoFonts[ eGuiFontLarge ] ) 
	{
		log_msg( 0, "DtwStateMenu::GameStateStartup: failed loading font_large.xml\n" );
		return -1;
	}
	// load images
	m_apoGuiImages[ eGuiImageMouseCursor ]			= m_poDriver->getTexture( GetAssetPathAndName( "cursor.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageBlack ]				= m_poDriver->getTexture( GetAssetPathAndName( "black.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageTextBoxCorner ]		= m_poDriver->getTexture( GetAssetPathAndName( "textbox_corner.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageTextBoxLeftEdge ]		= m_poDriver->getTexture( GetAssetPathAndName( "textbox_left_edge.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageTextBoxTopEdge ]		= m_poDriver->getTexture( GetAssetPathAndName( "textbox_top_edge.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageButton80x32 ]			= m_poDriver->getTexture( GetAssetPathAndName( "button_80x32.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageButton100x32 ]			= m_poDriver->getTexture( GetAssetPathAndName( "button_100x32.png", eGameAssetTypeGui ) );
	m_apoGuiImages[ eGuiImageButton128x32 ]			= m_poDriver->getTexture( GetAssetPathAndName( "button_128x32.png", eGameAssetTypeGui ) );
	//log_msg( 0, "8 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
	return 0;
}

//------------------------------------------------------------------------------
//! render a frame
void DtwStateMenu::RenderMenuFrame( void )
{
	m_poIrrBase->m_poTimer->tick();
	m_poDriver->beginScene( true, true, irr::video::SColor(255,0,0,0) );

	// render the scene
	m_poSceneMgr->drawAll();
	m_poGui->drawAll();
	// done rendering scene
	m_poDriver->endScene();
	static int iTicks;
	// every 1000 frames log the frames per second
	//if( iTicks > 1000 ) 
	//{
	//	iTicks = 1;
	//	int iFps = m_poDriver->getFPS();
	//	log_msg( 0, "FPS %d\n", iFps);
	//}
	//iTicks++;

}

//------------------------------------------------------------------------------
//! draw scene
void DtwStateMenu::Draw( void )
{
	DrawTutorialText();

}
